package work;

import java.text.MessageFormat;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import model.user;
import model.duty;
import model.database;


public class userDutyData {
	public static duty[] getUserDutyByCookie(user one)
	{
		database db = new database(
				"jdbc:mysql://localhost/gazi?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT&useSSL=false",
				"root",
				"123456");
		duty[] res = null;
		String sql = MessageFormat.format("select id, content, userPhone, isOpenMessageWarn,warnTime from duty where cookie = ''{0}''", one.getCookie());
		String[][] result = db.select("duty", sql);
		if (result != null)
		{
			res = new duty[result.length];
			for (int i = 0; i < result.length; i++)
			{
				int id = Integer.parseInt(result[i][0]);
				String content = result[i][1];
				String userPhone = result[i][2];
				int isOpenMessageWarn = Integer.parseInt(result[i][3]);
				String warnTime = result[i][4];
				res[i] = new duty(id, content, userPhone, isOpenMessageWarn, warnTime);
			}
		}
		
		return res;
	}
	public static duty[] getUserDuty(user one)
	{
		database db = new database(
				"jdbc:mysql://localhost/gazi?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT&useSSL=false",
				"root",
				"123456");
		duty[] res = null;
		String sql = MessageFormat.format("select id, content, userPhone, isOpenMessageWarn,warnTime from duty where userPhone = ''{0}''", one.getPhone());
		String[][] result = db.select("duty", sql);
		if (result != null)
		{
			res = new duty[result.length];
			for (int i = 0; i < result.length; i++)
			{
				int id = Integer.parseInt(result[i][0]);
				String content = result[i][1];
				String userPhone = result[i][2];
				int isOpenMessageWarn = Integer.parseInt(result[i][3]);
				String warnTime = result[i][4];
				res[i] = new duty(id, content, userPhone, isOpenMessageWarn, warnTime);
			}
		}
		
		return res;
	}
}
